/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import dao.HoadonDAO;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.Hoadon;

/**
 *
 * @author kydrenw
 */
public class ReportController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.reset();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=dshoadon.xls");

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Hoadon");

        sheet.setAutobreaks(true);

        HSSFRow header = sheet.createRow(0);
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillBackgroundColor(HSSFColor.GREY_50_PERCENT.index);
        cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
        //header.setRowStyle();
        header.createCell(0).setCellValue("Thời gian đặt hàng");
        header.createCell(1).setCellValue("Khách hàng");
        header.createCell(2).setCellValue("Người nhận");
        header.createCell(3).setCellValue("Tổng tiền");
        header.createCell(4).setCellValue("Tình trạng");

        List<Hoadon> ds = HoadonDAO.layDanhSach();

        for (int i = 0; i < ds.size(); i++) {
            Hoadon hd = ds.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(hd.getNgaydh());
            row.createCell(1).setCellValue(hd.getNguoidung().getTendangnhap());
            row.createCell(2).setCellValue(hd.getNguoinhan());
            row.createCell(3).setCellValue(hd.getTongtien());
            row.createCell(4).setCellValue(hd.getTinhtrang().getTen());
            for (int j = 0; j < 5; j++) {
                sheet.autoSizeColumn(i);
            }
        }

        ServletOutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();

        return null;
    }
}
